/*
    This file is part of FSJController.

    FSJController is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    FSJController is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FSJController.  If not, see <http://www.gnu.org/licenses/>.
*/

package de.qsc.fsjcontroller;

import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class represents a freeswitch channel
 * 
 * @author Andreas Mager <ama@qsc.de>
 *
 */
public abstract class FSChannel {

    static final Logger logger = LoggerFactory.getLogger(FSChannel.class);

    private UUID uuid;
    private boolean inbound;
    private String from;
    private String to;
    protected FSController controller;
    public FSController getController(){
        return controller;
    }
   
    public enum State {
        INIT, ACTIVE, DEAD;
    }
    private State state;

    public void setState(State state) {
        this.state = state;
    }
    
    public State getState() {
        return state;
    }
    
    
    /**
     * sets the uuid from this channel. be careful !! 
     * @param uuid
     */
    public void setUUID(UUID uuid) {
        this.uuid = uuid;
    }

    /**
     * 
     * @return the channel uuid
     */
    public UUID getUUID() {
        return uuid;
    }

    /**
     * set this call in or outbound
     * @param inbound true for inbound
     */
    public void setInbound(boolean inbound) {
        this.inbound = inbound;
    }

    /**
     * is this call in or outbound
     * @return inbound if true inbound
     */
    public boolean isInbound() {
        return inbound;
    }

    /**
     * @param from Destination number
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * 
     * @return Destination number
     */
    public String getFrom() {
        return from;
    }

    /**
     * 
     * @param target target phone number
     */
    public void setTo(String target) {
        this.to = target;
    }
    
    /**
     * 
     * @return target phone number
     */
    public String getTo() {
        return to;
    }

    /**
     * Creates a freeswitch channel representation and ties it to the freeswitch controller 
     * 
     * @param uuid channel uuid
     * @param controller 
     */
    public FSChannel(UUID uuid, FSController controller, String from, String to, boolean inbound) {
        this.uuid = uuid;
        this.controller = controller;
        state = State.INIT;
        setFrom(from);
        setTo(to);
        setInbound(inbound);
    }

    /**
     * executes a freeswitch application on this channel
     * 
     * @param appName freeswitch application name
     * @param appArgs arguments of this freeswitch application
     */
    public void runApplication(final String appName, final String appArgs) {
        if (state != State.ACTIVE) {
            logger.warn("channel " + uuid + " has state {}, application ignored: {}", state, appName);
            return;
        }
        controller.queueCommand(new FSApplicationCommand(uuid, appName, appArgs));
    }
    
    @Override
    public String toString() {
        return "FSChannel [uuid=" + uuid+ ", inbound=" + inbound + ", state=" + state + ", from=" + from + ", to=" + to + "]";
    }

    /**
     * This method will be invoked by the controller if an outgoing call fails
     */
    abstract public void onFailure();
    
    /**
     * This method will be invoked each time the {@link FSController} receives event for this channel.
     * 
     * @param event
     */
    abstract public void onEvent(FSEvent event);

}
